package com.qin.service.impl;

import java.util.List;

import com.qin.dao.ShoppingDao;
import com.qin.entity.Shopping;
import com.qin.service.ShoppingService;

public class ShoppingServiceImpl implements ShoppingService {

	private ShoppingDao shoppingDao = new ShoppingDao();
	
	@Override
	public Long insertShopping(Shopping shopping) {
		return shoppingDao.insertShopping(shopping);
	}

	@Override
	public int logicDeleteShopping(Long id) {
		return shoppingDao.deleteShopping(id);
	}

	@Override
	public int updateShopping(Long id, Integer num) {
		return shoppingDao.updateShopping(id, num);
	}

	@Override
	public Shopping getShoppingById(Long id) {
		return shoppingDao.queryShoppingById(id);
	}

	@Override
	public List<Shopping> getShoppingItemsByUserId(Long id) {
		return shoppingDao.queryShoppingByUserId(id);
	}

	@Override
	public Shopping queryShoppingByUserAndBook(Long uid, Long bid) {
		return shoppingDao.queryShoppingByUserAndBook(uid, bid);
	}

	@Override
	public Long insertShoppingPlus(Shopping shopping, Long uid, Long bid) {
		// 先去查询看看是否有
		Shopping queryShoppingByUserAndBook = queryShoppingByUserAndBook(uid,bid);
		if(queryShoppingByUserAndBook != null) { // 代表有
			// 拿到图书数量的变换量,肯定是+
			Integer bookNumChange = queryShoppingByUserAndBook.getBookNum()+shopping.getBookNum();
			// 进行修改,0代表失败
			int updateShopping = updateShopping(shopping.getId(),bookNumChange);
			
			return updateShopping==0?-1L:0L;
		}else {
			// 代表没有，可以正常插入
			Long insertShopping = insertShopping(shopping);
			
			// 返回自增id
			return insertShopping;
		}
	}

}
